package com.sinitek.sirm.elfinder.service.impl;

import br.com.trustsystems.elfinder.ElFinderConstants;
import br.com.trustsystems.elfinder.configuration.jaxb.ElfinderConfiguration;
import br.com.trustsystems.elfinder.core.Target;
import br.com.trustsystems.elfinder.core.Volume;
import br.com.trustsystems.elfinder.core.VolumeSecurity;
import br.com.trustsystems.elfinder.core.impl.DefaultVolumeSecurity;
import br.com.trustsystems.elfinder.core.impl.SecurityConstraint;
import br.com.trustsystems.elfinder.service.ElfinderStorage;
import br.com.trustsystems.elfinder.service.ThumbnailWidth;
import br.com.trustsystems.elfinder.service.VolumeSources;
import br.com.trustsystems.elfinder.service.impl.DefaultElfinderStorage;
import br.com.trustsystems.elfinder.service.impl.DefaultThumbnailWidth;
import br.com.trustsystems.elfinder.support.locale.LocaleUtils;
import com.sinitek.sirm.elfinder.configuration.ElfinderDatabaseConfigurationWrapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * elfinder数据库服务实现方案
 * @author yg.huang
 * @version v1.0
 *          DATE  2017/8/15
 */

public class ElfinderDatabaseStorage extends DefaultElfinderStorage implements InitializingBean {

    private   ElfinderDatabaseConfigurationWrapper elfinderConfiguration;




    @Autowired()
    public void setElfinderConfiguration(ElfinderDatabaseConfigurationWrapper elfinderConfiguration) {
        this.elfinderConfiguration = elfinderConfiguration;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // creates thumbnail
        DefaultThumbnailWidth defaultThumbnailWidth = new DefaultThumbnailWidth();
        defaultThumbnailWidth.setThumbnailWidth(elfinderConfiguration.getThumbnailWidth());
        this.setThumbnailWidth(defaultThumbnailWidth);
        this.setTempDir(elfinderConfiguration.getTempDir());




        // creates volumes, volumeIds, volumeLocale and volumeSecurities
        Character defaultVolumeId = 'A';
        List<ElfinderConfiguration.Volume> elfinderConfigurationVolumes = elfinderConfiguration.getVolumes();
        List<Volume> elfinderVolumes = new ArrayList<>(elfinderConfigurationVolumes.size());
        Map<Volume, String> elfinderVolumeIds = new HashMap<>(elfinderConfigurationVolumes.size());
        Map<Volume, Locale> elfinderVolumeLocales = new HashMap<>(elfinderConfigurationVolumes.size());
        List<VolumeSecurity> elfinderVolumeSecurities = new ArrayList<>();

        // creates volumes
        for (ElfinderConfiguration.Volume elfinderConfigurationVolume : elfinderConfigurationVolumes) {

            final String alias = elfinderConfigurationVolume.getAlias();
            final String path = elfinderConfigurationVolume.getPath();
            final String source = elfinderConfigurationVolume.getSource();
            final String locale = elfinderConfigurationVolume.getLocale();
            final boolean isLocked = elfinderConfigurationVolume.getConstraint().isLocked();
            final boolean isReadable = elfinderConfigurationVolume.getConstraint().isReadable();
            final boolean isWritable = elfinderConfigurationVolume.getConstraint().isWritable();

            // creates new volume
            Volume volume = VolumeSources.of(source).newInstance(alias, path);

            elfinderVolumes.add(volume);
            elfinderVolumeIds.put(volume, Character.toString(defaultVolumeId));
            elfinderVolumeLocales.put(volume, LocaleUtils.toLocale(locale));

            // creates security constraint
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setLocked(isLocked);
            securityConstraint.setReadable(isReadable);
            securityConstraint.setWritable(isWritable);

            // creates volume pattern and volume security
            final String volumePattern = Character.toString(defaultVolumeId) + ElFinderConstants.ELFINDER_VOLUME_SERCURITY_REGEX;
            elfinderVolumeSecurities.add(new DefaultVolumeSecurity(volumePattern, securityConstraint));

            // prepare next volumeId character
            defaultVolumeId++;
        }
        this.setVolumes(elfinderVolumes);
        this.setVolumeIds(elfinderVolumeIds);
        this.setVolumeLocales(elfinderVolumeLocales);
        this.setVolumeSecurities(elfinderVolumeSecurities);

    }
}
